package com.block.datapush.form.item;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.service.LogFactory;
import com.block.datapush.form.enums.PushResultEnum;
import com.block.datapush.form.item.util.BaiXiangRongUtil;
import com.block.datapush.form.item.util.ZiWeiHuaUtil;
import com.block.datapush.form.service.IFormPushHandle;
import com.block.datapush.model.dto.FormPushResultDTO;
import com.block.db.entity.FormInfo;
import com.block.db.entity.FormInfoHis;
import com.block.db.entity.MemberExtInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 佰享融
 */
@Service
public class BaiXiangRongHandleService implements IFormPushHandle {

    private static final Logger log = LogFactory.getPushLog();

    @Override
    public boolean queryMatchResult(FormInfo formInfo, MemberExtInfo memberExtInfo) throws Exception {

        String queryUrl = formInfo.getQueryUrl();
        String mobileMd5 = SecureUtil.md5(memberExtInfo.getMobile()).toLowerCase();

        Map<String, Object> bizParamMap = new HashMap<>();
        bizParamMap.put("phoneMd5", mobileMd5);

        if(StrUtil.isEmpty(memberExtInfo.getIdCard())){
            return false;
        }

        log.info("【佰享融】撞库开始【queryUrl】{}【bizParam】{}", queryUrl, JSON.toJSONString(bizParamMap));
        String result = HttpUtil.createPost(queryUrl)
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(bizParamMap))
                .execute().body();
        log.info("【佰享融】撞库结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        Integer code = resultObject.getInteger("code");
        String msg = resultObject.getString("msg");

        if (ObjectUtil.equal(code, Integer.valueOf(500))) {
            return true;
        }
        return false;
    }

    @Override
    public FormPushResultDTO pushMemberInfo(FormInfo formInfo, MemberExtInfo memberExtInfo, String orderNo, JSONObject jsonObject) throws Exception {

        FormPushResultDTO pushResultDTO = new FormPushResultDTO();
        pushResultDTO.setOrderNo(orderNo);

        String queryUrl = formInfo.getRequestUrl();
        Map<String, Object> bizParamMap = new HashMap<>();
        bizParamMap.put("channel",formInfo.getApiCode());
        bizParamMap.put("phone",memberExtInfo.getMobile());
        bizParamMap.put("idnum",memberExtInfo.getIdCard());
        bizParamMap.put("city",memberExtInfo.getCity());
        bizParamMap.put("name",memberExtInfo.getUsername());
        bizParamMap.put("loan", BaiXiangRongUtil.getLoanAmount(memberExtInfo.getLoanAmount()));
        bizParamMap.put("age",memberExtInfo.getAge());
        bizParamMap.put("professional",BaiXiangRongUtil.getJob(memberExtInfo.getJob()));
        bizParamMap.put("zhima_credit",BaiXiangRongUtil.getZhima(memberExtInfo.getZhimaScore()));
        bizParamMap.put("has",BaiXiangRongUtil.getSheBao(memberExtInfo.getSocialInsurance()));
        bizParamMap.put("fund",BaiXiangRongUtil.getGjj(memberExtInfo.getGjj()));
        bizParamMap.put("house_property",BaiXiangRongUtil.getHouse(memberExtInfo.getHouse()));
        bizParamMap.put("car_property",BaiXiangRongUtil.getCar(memberExtInfo.getCar()));
        bizParamMap.put("huabei",BaiXiangRongUtil.getCar(memberExtInfo.getCar()));
        bizParamMap.put("baitiao",BaiXiangRongUtil.getBaitiao(memberExtInfo.getBaitiao()));

        log.info("【佰享融】导单开始【queryUrl】{}【bizParamMap】{}", queryUrl, JSON.toJSONString(bizParamMap));
        String result = HttpUtil.createPost(queryUrl)
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(bizParamMap))
                .execute().body();
        log.info("【佰享融】导单结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        Integer code = resultObject.getInteger("code");
        String msg = resultObject.getString("msg");


        pushResultDTO.setResult(result);
        pushResultDTO.setCode(String.valueOf(code));
        pushResultDTO.setMessage(msg);
        pushResultDTO.setParamContent(JSON.toJSONString(bizParamMap));
        if (ObjectUtil.equal(code, Integer.valueOf(500))) {
            pushResultDTO.setSuccess(true);
            pushResultDTO.setPushResult(PushResultEnum.SUCCESS.getType());
        } else {
            pushResultDTO.setPushResult(PushResultEnum.FAILED.getType());
            pushResultDTO.setSuccess(false);
        }
        return pushResultDTO;
    }



    @Override
    public void afterPushHandle(FormInfoHis formInfoHis) {

    }

}
